package com.bdqn.t382.lx.controller;

import com.alibaba.fastjson2.JSON;
import com.bdqn.t382.lx.pojo.BookManage;
import com.bdqn.t382.lx.pojo.Page;
import com.bdqn.t382.lx.pojo.SysUser;
import com.bdqn.t382.lx.service.BookManageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookManageController {
    //定义日志
    private static final Logger logger = LoggerFactory.getLogger(BookManageController.class);

    @Autowired
    private BookManageService bookManageService;

    /**
     * 获取图书列表
     *
     * @return 图书列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public List<BookManage> getBookList(Model model) {
        List<BookManage> list = bookManageService.getBookManageList();
        model.addAttribute("list", list);
        logger.info("获取图书列表成功");
        return list;
    }

    /**
     * 跳转到添加图书页面
     *
     * @return 页面路径
     */
    @RequestMapping("/add")
    public String toAddPage(Model model) {
        logger.info("跳转到添加图书页面");
        return "add";
    }

    /**
     * 添加图书
     *
     * @param bookManage 图书信息
     * @return 影响行数
     */
    @ResponseBody
    @RequestMapping("/formAdd")
    public int addBook(@RequestBody BookManage bookManage) {
        int count = bookManageService.insertBookManage(bookManage);
        logger.info("添加图书成功，影响行数：{}", count);
        return count;
    }

    /**
     * 删除图书
     *
     * @param b_id 图书ID
     * @return 影响行数
     */
    @ResponseBody
    @RequestMapping("/delete")
    public int deleteBook(@RequestParam int b_id) {
        int count = bookManageService.deleteBookManage(b_id);
        logger.info("删除图书成功，影响行数：{}", count);
        return count;
    }

    /**
     * 查看图书详情
     *
     * @param b_id 图书ID
     * @return 图书信息
     */
    @ResponseBody
    @RequestMapping("/view")
    public BookManage viewBook(@RequestParam int b_id) {
        BookManage book = bookManageService.viewBookManage(b_id);
        logger.info("查看图书详情成功");
        return book;
    }

    /**
     * 更新图书信息
     *
     * @param bookManage 图书信息
     * @return 影响行数
     */
    @ResponseBody
    @RequestMapping("/formUpdate")
    public int updateBook(@RequestBody BookManage bookManage) {
        int count = bookManageService.updateBookManage(bookManage);
        logger.info("更新图书信息成功，影响行数：{}", count);
        return count;
    }

    /**
     * 分页查询图书
     *
     * @param pageNo   页码
     * @param pageSize 每页大小
     * @param b_name   图书名称
     * @return 分页信息
     */
    @ResponseBody
    @RequestMapping("/page")
    public Page getPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize, @RequestParam String b_name) {
        Page page = bookManageService.getPage(pageNo, pageSize, b_name);
        logger.info("分页查询图书成功");
        return page;
    }

    /**
     * 跳转到登录页面
     *
     * @return 页面路径
     */
    @RequestMapping("/login")
    public String toLoginPage(Model model) {
        logger.info("跳转到登录页面");
        return "login";
    }

    /**
     * 执行登录
     *
     * @param sysUser 用户信息
     * @return 用户信息
     */
    @ResponseBody
    @RequestMapping("/doLogin")
    public SysUser doLogin(SysUser sysUser) {
        SysUser user = bookManageService.getLogin(sysUser.getAccount(), sysUser.getPassword());
        logger.info("执行登录成功");
        return user;
    }

    /**
     * 根据ID获取用户信息
     *
     * @param id 用户ID
     * @return 用户信息
     */
    @ResponseBody
    @RequestMapping("/getSysuserById")
    public SysUser getSysuserById(@RequestParam int id) {
        SysUser user = bookManageService.getSysuserById(id);
        logger.info("根据ID获取用户信息成功");
        return user;
    }

    /**
     * 跳转到保存页面
     *
     * @return 页面路径
     */
    @RequestMapping("/save")
    public String toSavePage(Model model) {
        logger.info("跳转到保存页面");
        return "save";
    }

    /**
     * 检查账号是否存在
     *
     * @param sysUser 用户信息
     * @return 检查结果
     */
    @ResponseBody
    @RequestMapping("/accountExist")
    public String checkAccountExist(SysUser sysUser) {
        HashMap<String, Object> resultMap = new HashMap<>();
        SysUser login = bookManageService.getLogin(sysUser.getAccount(), sysUser.getPassword());
        if (login != null) {
            resultMap.put("exist", 1);
        } else {
            resultMap.put("exist", 0);
        }
        logger.info("检查账号是否存在成功");
        return JSON.toJSONString(resultMap);
    }
}